package cn.itcast.controller;

import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.User;
import cn.itcast.response.PageResults;
import cn.itcast.response.StatusCode;
import cn.itcast.service.UserService;
import cn.itcast.response.ResponseResult;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping
    public ResponseResult<User>add(@RequestBody User user) {

        if (true){
            throw new IndexOutOfBoundsException("御姐");
        }
        return ResponseResult.okResult(userService.add(user));
    }

    /**
     * 分页查询
     * @param user
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public ResponseResult<User>findPage(@RequestBody User user, 	@PathVariable("page") Integer page,
                                        @PathVariable("size") Integer pageSize) {

        Page<User>pages= userService.findPage(user,page,pageSize);

        //数据列表  总记录  总页数
        PageResults pageResults=new PageResults<>(pages.getTotal(),pages.getPages(),pages.getPageSize());

        return new ResponseResult<>(StatusCode.QUERY_PAGE_SUCCESS,"查询成功",pageResults);
    }

}
